import logging
import os
import sys
from logging.handlers import TimedRotatingFileHandler

import redis

from const import cfg

log_path = f"{os.path.dirname(__file__)}/../logs"
if not os.path.exists(log_path):
    os.mkdir(log_path)
handlers = [TimedRotatingFileHandler(f'{log_path}/my_mirai.log', when="midnight", backupCount=5, encoding="utf-8"),
            logging.StreamHandler(sys.stdout)]
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s [%(levelname)s] [%(module)s-%(threadName)s]  %(lineno)d - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    handlers=handlers
)
logging.info("日志 > %s", os.path.abspath(log_path))

logger = logging.getLogger()

REDIS_POOL = None

if "redis" in cfg:
    host = cfg["redis"].get("host") or "192.168.227.129"
    port = cfg["redis"].get("port") or 6379
    db = 1
    REDIS_POOL = redis.ConnectionPool(host=host, port=port, db=db)
